home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2933 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.3 KB  |  50 lines

  1. Path: news.bridge.net!news
  2. From: David Byrden <100101.2547@compuserve.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Problem with generic classes/templates
  5. Date: 20 Jan 1996 18:45:45 GMT
  6. Organization: self-employed
  7. Message-ID: <4drd8p$2sa@news.bridge.net>
  8. References: <4dlerb$r3r@finch.doc.ic.ac.uk>
  9. NNTP-Posting-Host: ppp-mia1-60.bridge.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
  14.  
  15.  
  16.  
  17. >> template <class X> class marzipan<X>{
  18. >>   X ian;
  19. >>   public:
  20. >>   marzipan<X>(/* what goes here? */) { /* and what goes here? */ }
  21. >> }
  22.  
  23.  
  24. >> But how do I pass parameters to the constructor function of meringue?
  25.  
  26. >>  I understand that I can use "..." somehow
  27.  
  28. Using the ellipsis would abandon the compiler's type checking. Your ctor 
  29. would be as unreliable as 'printf'. It would also require you 
  30. to explicitly write specialisations of the ctor for every possible type 
  31. X.
  32.  
  33. The only general solution is to assume that the class X knows how to copy 
  34. its own values, i.e. it has a public copy constructor. This assumption is 
  35. made throughout the STL.
  36.  
  37.  
  38.  template <class X> class marzipan<X>{
  39.    X ian;
  40.    public:
  41.    marzipan<X>( const X& init_value )
  42.       :  ian(init_value) {  }
  43.  
  44.  } ;
  45.  
  46.                   David
  47.  
  48.  
  49.  
  50.